package com.hugoo.core.util;

/**
 *
 * @author Lovelock.Chan
 */
public class LxString {

    final static char[] digits = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
        'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
        'U', 'V', 'W', 'X', 'Y', 'Z', '_', 'a', 'b', 'c',
        'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
        'x', 'y', 'z'
    };
    private static int c = 1;
    private static long t = System.currentTimeMillis();

    private static synchronized EP tme() {
        long x = System.currentTimeMillis();
        if (x <= t) {
            if (++c > 3968) {
                t += 1;
                c = 0;
            }
        } else {
            t = x;
            c = 0;
        }
        return new EP(c, t);
    }

    private static String scod(final int radix) {
        final char[] buf = new char[65];
        int charPos = 64;
        EP e = tme();
        long i = e.p;
        int x = e.e, f = x;
        while (x >= radix) {
            buf[charPos--] = digits[x % radix];
            x = x / radix;
        }
        buf[charPos--] = digits[(int) x];
        if (f < 63) {
            buf[charPos--] = '0';
        }
        while (i >= radix) {
            buf[charPos--] = digits[(int) (i % radix)];
            i = i / radix;
        }
        buf[charPos] = digits[(int) i];
        return new String(buf, charPos, (65 - charPos));
    }

    public static String scod() {
        return scod(63);
    }

    static class EP {

        final int e;
        final long p;

        public EP(int e, long p) {
            this.e = e;
            this.p = p;
        }
    }
}
